<template>
  <div class="mod-user">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item label="订单编号">
        <el-input v-model="dataForm.orderNum" placeholder="订单编号" clearable></el-input>
      </el-form-item>
      <el-form-item label="手机号">
        <el-input v-model="dataForm.mobile" placeholder="手机号" clearable></el-input>
      </el-form-item>
      <el-form-item label="支付状态">
        <el-select v-model="dataForm.payStatus" style="width:150px;">
            <el-option v-for="item in payStatusArr"
                       :label="item.text"
                       :value="item.value"
                       :key="item.value">
            </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="支付方式">
          <el-select v-model="dataForm.payType" style="width: 150px">
              <el-option v-for="item in payTypeArr" :label="item.text" :key="item.value" :value="item.value"></el-option>
          </el-select>
      </el-form-item>
      <el-form-item label="生效状态">
        <el-select v-model="dataForm.activeState" style="width: 150px;">
            <el-option v-for="item in activeStateArr" :label="item.text" :key="item.value" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">

      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="orderNum"
        header-align="center"
        align="center"
        label="订单编号">
      </el-table-column>
      <el-table-column
        prop="mobile"
        header-align="center"
        align="center"
        label="手机号">
      </el-table-column>
      <el-table-column
        prop="payStatus"
        header-align="center"
        align="center"
        label="支付状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.payStatus === 0" size="small" type="danger">待支付</el-tag>
          <el-tag v-if="scope.row.payStatus === 1"  size="small">已支付</el-tag>
          <el-tag v-if="scope.row.payStatus === 2"  size="small">已退款</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="payType"
        header-align="center"
        align="center"
        label="支付方式">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.payType === 1" size="small" type="danger">微信</el-tag>
          <el-tag v-if="scope.row.payType === 2"  size="small">支付宝</el-tag>
          <el-tag v-if="scope.row.payType === 3"  size="small">银行转账</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="activeState"
        header-align="center"
        align="center"
        label="生效状态">
          <template slot-scope="scope">
              <el-tag v-if="scope.row.activeState === 0" size="small" type="danger">未开通</el-tag>
              <el-tag v-if="scope.row.activeState === 1"  size="small">已生效</el-tag>
              <el-tag v-if="scope.row.activeState === 2"  size="small">待生效</el-tag>
              <el-tag v-if="scope.row.activeState === 3"  size="small">已过期</el-tag>
          </template>
      </el-table-column>
      <el-table-column
          prop="productName"
          header-align="center"
          align="center"
          label="产品名称">
      </el-table-column>
      <el-table-column
        prop="price"
        header-align="center"
        align="center"
        label="订单金额">
      </el-table-column>
      <el-table-column
        prop="payTime"
        header-align="center"
        align="center"
        label="支付时间">
      </el-table-column>
        <el-table-column
          prop="startTime"
          header-align="center"
          align="center"
          label="开始时间">
        </el-table-column>
        <el-table-column
          prop="endTime"
          header-align="center"
          align="center"
          label="结束时间">
        </el-table-column>
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        width="180"
        label="下单时间">
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <order-detail  v-if="orderDetailVisible" ref="orderDetail" @refreshDataList="getDataList"></order-detail>
  </div>
</template>

<script>
  import orderDetailVue from './order-detail'
  export default {
    data () {
      return {
        dataForm: {
          orderNum:'',
          mobile:'',
          payStatus:'',
          payType:'',
          activeState:''
        },
        dataList: [],
        payStatusArr:[
          {text:"请选择",value:''},
          {text:"待支付",value:0},
          {text:"已支付",value:1},
          {text:"已取消",value:2}
        ],
        payTypeArr:[
          {text:"请选择",value:''},
          {text:"微信",value:1},
          {text:"支付宝",value:2},
          {text:"银行转账",value:3}
        ],
        activeStateArr:[
          {text:"请选择",value:''},
          {text:"未开通",value:"0"},
          {text:"已生效",value:"1"},
          {text:"待生效",value:"2"},
          {text:"已失效",value:"3"}
        ],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        orderDetailVisible: false
      }
    },
    components: {
      orderDetailVue
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/cOrder/findList'),
          method: 'post',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'orderNum': this.dataForm.orderNum,
            'mobile':this.dataForm.mobile,
            'payType':this.dataForm.payType,
            'payStatus':this.dataForm.payStatus,
            'activeState':this.dataForm.activeState
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.orderDetailVisible = true
        this.$nextTick(() => {
          this.$refs.orderDetail.init(id)
        })
      },
      // 删除
      deleteHandle (id) {
        var userIds = id ? [id] : this.dataListSelections.map(item => {
          return item.userId
        })
        this.$confirm(`确定对[id=${userIds.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/sys/user/delete'),
            method: 'post',
            data: this.$http.adornData(userIds, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }).catch(() => {})
      }
    }
  }
</script>
